package com.localsocial.screenphone;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.localsocial.ds.Person;

public class ImageAdapter extends BaseAdapter {
    
    private Activity activity;
    private String[] data;
    private List<Person> people;
    private static LayoutInflater inflater = null;
    public ImageLoader imageLoader; 
    
    public ImageAdapter(Activity a, List<Person> p) {
        this.activity = a;
        this.people = p;
        this.data = getAllUrls();
        this.inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.imageLoader = new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
    	return people.size();
        //return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public static class ViewHolder{
        public TextView text;
        public ImageView image;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;
        if(convertView == null){
            vi = inflater.inflate(R.layout.person, null);
            holder = new ViewHolder();
            holder.text = (TextView)vi.findViewById(R.id.name);;
            holder.image = (ImageView)vi.findViewById(R.id.image);
            vi.setTag(holder);
        } else {
            holder = (ViewHolder)vi.getTag();
        }
        
        holder.text.setText(people.get(position).getFullName());
        holder.image.setTag(data[position]);
        imageLoader.DisplayImage(data[position], activity, holder.image);
        return vi;
    }
    
	/**
	 * Return String array of url strings of all connections
	 */
	public String[] getAllUrls() {
		String[] urls = new String[people.size()];
		
		for (int i=0; i<people.size(); i++) {
			urls[i] = people.get(i).getPictureUrl();
		}
				
		return urls;
	}
}